package com.pf.controller;

import com.pf.dto.BaseResult;
import com.pf.service.ElasticSearchService;
import com.pf.utils.BaseResultUtils;
import com.pf.utils.ElasticSearchUtils;
import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author PanFei
 * @version 1.0.0
 * @createTime 2021/7/27
 */
@RestController
@RequestMapping("/elasticSearch")
public class ElasticSearchController {

    @Autowired
    private ElasticSearchService elasticSearchService;

    /**
     * 初始化索引以及数据
     * <p>
     * 测试：http://localhost:8080/elasticSearch/initIndex?indexName=commodity
     *
     * @return
     */
    @GetMapping("/initIndex")
    public BaseResult initIndex(@RequestParam("indexName") String indexName) {
        elasticSearchService.initIndex(indexName);
        return BaseResultUtils.success();
    }

    /**
     * 检查是否已存在某索引
     * <p>
     * 测试：http://localhost:8080/elasticSearch/checkExistsIndex?indexName=commodity
     *
     * @return true:已存在  false：不存在
     */
    @GetMapping("/checkExistsIndex")
    public BaseResult<Boolean> checkExistsIndex(@RequestParam("indexName") String indexName) {
        return BaseResultUtils.success(ElasticSearchUtils.isExistingIndex(indexName));
    }

    /**
     * 删除索引
     * <p>
     * 测试：http://localhost:8080/elasticSearch/deleteIndex?indexName=commodity
     *
     * @param indexName
     * @return
     */
    @GetMapping("/deleteIndex")
    public BaseResult<Boolean> deleteIndex(@RequestParam("indexName") String indexName) {
        return BaseResultUtils.success(ElasticSearchUtils.deleteIndex(indexName));
    }

    /**
     * 为某索引添加数据
     * <p>
     * 测试：http://localhost:8080/elasticSearch/initData?indexName=commodity
     *
     * @return
     */
    @GetMapping("/initData")
    public BaseResult initData(@RequestParam("indexName") String indexName) {
        elasticSearchService.initData(indexName);
        return BaseResultUtils.success();
    }

    /**
     * 返回某索引的所有数据
     * <p>
     * 测试：http://localhost:8080/elasticSearch/getAllData?indexName=commodity
     *
     * @param indexName
     * @return
     */
    @GetMapping("/getAllData")
    public BaseResult<SearchHits> getAllDataByIndexName(@RequestParam("indexName") String indexName) {
        return BaseResultUtils.success(ElasticSearchUtils.getAllData(indexName));
    }

    /**
     * 批量修改商品的销量信息
     * <p>
     * 因为商品销量会随时变化，所以该方法应该定时执行一次。修改es中商品的销量信息
     * <p>
     * 测试：http://localhost:8080/elasticSearch/batchUpdateSales?indexName=commodity
     *
     * @param indexName
     * @return
     */
    @GetMapping("/batchUpdateSales")
    public BaseResult batchUpdateSales(@RequestParam("indexName") String indexName) {
        elasticSearchService.updateSales(indexName);
        return BaseResultUtils.success();
    }

    @GetMapping("/getDataByParams")
    public BaseResult<SearchHits> getDataByParams(@RequestParam("indexName") String indexName) {
        return BaseResultUtils.success(ElasticSearchUtils.queryByParams(indexName, null, "sales"));
    }


}
